#include "stm32f10x.h"                  // Device header
#include "MyRTC.h"
#include "OLED.h"
#include "Key.h"

/***************************时间设置界面********************************/
void Show_SetTime_FirstUI(void)		//第一页显示年月日
{
	OLED_ShowImage(0,0,16,16,Image_Return);
	OLED_Printf(0,16,OLED_8X16,"年:%4d",MyRTC_Time[0]);
	OLED_Printf(0,32,OLED_8X16,"月:%2d",MyRTC_Time[1]);
	OLED_Printf(0,48,OLED_8X16,"日:%2d",MyRTC_Time[2]);

}
void Show_SetTime_SecondUI(void)	//第二页显示时分秒
{
	OLED_Printf(0,0,OLED_8X16,"时:%2d",MyRTC_Time[3]);
	OLED_Printf(0,16,OLED_8X16,"分:%2d",MyRTC_Time[4]);
	OLED_Printf(0,32,OLED_8X16,"秒:%2d",MyRTC_Time[5]);
}

/*
	函数：更改MyRTC_Time的值
	参数：i为MyRTC_Time[]下标
		flag为标志位
	返回值：无
*/
void Change_MyRTC_Time(uint8_t i,uint8_t flag)
{
	if(flag==1){MyRTC_Time[i]++;}
	else {MyRTC_Time[i]--;}
	MyRTC_SetTime();
}



extern uint8_t KeyNum;		//获取按键标志位以进行按键操作


uint8_t SetYear(void)		//更改年份
{
	//OLED_ShowChar(48,0,'A',OLED_6X8);		//测试是否进入此函数
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			Change_MyRTC_Time(0,1);
		}else if(KeyNum==2)
		{
			Change_MyRTC_Time(0,0);
		}else if(KeyNum==3)
		{
			return 0;
		}
		Show_SetTime_FirstUI();
		OLED_ReverseArea(24,16,32,16);
		OLED_Update();
	}
}

uint8_t SetMonth(void)
{
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			Change_MyRTC_Time(1,1);
			if(MyRTC_Time[1]>=13){MyRTC_Time[1]=1;MyRTC_SetTime();}
		}else if(KeyNum==2)
		{
			Change_MyRTC_Time(1,0);
			if(MyRTC_Time[1]<=0){MyRTC_Time[1]=12;MyRTC_SetTime();}
		}else if(KeyNum==3)
		{
			return 0;
		}
		Show_SetTime_FirstUI();
		OLED_ReverseArea(24,32,16,16);
		OLED_Update();
	}
}

uint8_t SetDay(void)
{
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			Change_MyRTC_Time(2,1);
			if(MyRTC_Time[2]>=32){MyRTC_Time[2]=1;MyRTC_SetTime();}
		}else if(KeyNum==2)
		{
			Change_MyRTC_Time(2,0);
			if(MyRTC_Time[2]<=0){MyRTC_Time[2]=31;MyRTC_SetTime();}
		}else if(KeyNum==3)
		{
			return 0;
		}
		Show_SetTime_FirstUI();
		OLED_ReverseArea(24,48,16,16);
		OLED_Update();
	}
}

uint8_t SetHour(void)
{
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			Change_MyRTC_Time(3,1);
			if(MyRTC_Time[3]>=24){MyRTC_Time[3]=0;MyRTC_SetTime();}
		}else if(KeyNum==2)
		{
			Change_MyRTC_Time(3,0);
			if(MyRTC_Time[3]<0){MyRTC_Time[3]=23;MyRTC_SetTime();}
		}else if(KeyNum==3)
		{
			return 0;
		}
		Show_SetTime_SecondUI();
		OLED_ReverseArea(24,0,16,16);
		OLED_Update();
	}
}

uint8_t SetMin(void)
{
	
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			Change_MyRTC_Time(4,1);
			if(MyRTC_Time[4]>=60){MyRTC_Time[4]=0;MyRTC_SetTime();}
		}else if(KeyNum==2)
		{
			Change_MyRTC_Time(4,0);
			if(MyRTC_Time[4]<0){MyRTC_Time[4]=59;MyRTC_SetTime();}
		}else if(KeyNum==3)
		{
			return 0;
		}
		Show_SetTime_SecondUI();
		OLED_ReverseArea(24,16,16,16);
		OLED_Update();
	}
}

uint8_t SetSec(void)
{
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			Change_MyRTC_Time(5,1);
			if(MyRTC_Time[5]>=60){MyRTC_Time[5]=0;MyRTC_SetTime();}
		}else if(KeyNum==2)
		{
			Change_MyRTC_Time(5,0);
			if(MyRTC_Time[5]<0){MyRTC_Time[5]=59;MyRTC_SetTime();}
		}else if(KeyNum==3)
		{
			return 0;
		}
		Show_SetTime_SecondUI();
		OLED_ReverseArea(24,32,16,16);
		OLED_Update();
	}
}

uint8_t set_time_flag=1;	//时间设置页面标志位
uint8_t Show_Setting_Time(void)
{
	while(1)
	{
		//循环获取标志位set_time_flag
		KeyNum=Key_GetNum();
		uint8_t set_time_flag_temp=0;
		if(KeyNum==1)		//上一项
		{	
			set_time_flag--;
			if(set_time_flag<=0)set_time_flag=7;
		}else if(KeyNum==2)	//下一项
		{	
			set_time_flag++;
			if(set_time_flag>=8)set_time_flag=1;
			
		}else if(KeyNum==3)	//确定
		{	
			OLED_Clear();
			OLED_Update();
			set_time_flag_temp=set_time_flag;
		}
		
		//根据标志位设置时间
		if(set_time_flag_temp==1){return 0;}
		else if(set_time_flag_temp==2){SetYear();}//年
		else if(set_time_flag_temp==3){SetMonth();}//月
		else if(set_time_flag_temp==4){SetDay();}//日
		else if(set_time_flag_temp==5){SetHour();}//时
		else if(set_time_flag_temp==6){SetMin();}//分
		else if(set_time_flag_temp==7){SetSec();}//秒
		
		//根据标志位，光标移到年月日时分秒上
		switch(set_time_flag)
		{
			case 1:						//返回键
				OLED_Clear();
				Show_SetTime_FirstUI();
				OLED_ReverseArea(0,0,16,16);
				OLED_Update();
				break;
			case 2:						//年
				OLED_Clear();
				Show_SetTime_FirstUI();
				OLED_ReverseArea(0,16,16,16);
				OLED_Update();
				break;
			case 3:						//月
				OLED_Clear();
				Show_SetTime_FirstUI();
				OLED_ReverseArea(0,32,16,16);
				OLED_Update();
				break;
			case 4:						//日
				OLED_Clear();
				Show_SetTime_FirstUI();
				OLED_ReverseArea(0,48,16,16);
				OLED_Update();
				break;
			case 5:						//时
				OLED_Clear();
				Show_SetTime_SecondUI();
				OLED_ReverseArea(0,0,16,16);
				OLED_Update();
				break;
			case 6:						//分
				OLED_Clear();
				Show_SetTime_SecondUI();
				OLED_ReverseArea(0,16,16,16);
				OLED_Update();
				break;
			case 7:						//秒
				OLED_Clear();
				Show_SetTime_SecondUI();
				OLED_ReverseArea(0,32,16,16);
				OLED_Update();
				break;
			
		}
	
	}
}
